<template>
  <el-dialog :title="info.title" :visible.sync="visible" center width="1460px" :before-close="handleCancel">
    <div class="">
      <el-row>
        <el-col :span="24">
          <div class="page-filter mb10 fl">
            <el-form ref="filter" :model="filter" label-width="0">
              <el-form-item class="mr10 mb5 fl" prop="begin_time">
                <el-date-picker
                  v-model="filter.begin_time"
                  placeholder="开始时间"
                  type="date"
                  format="yyyy-MM-dd"
                  value-format="yyyy-MM-dd"
                  size="small"
                  clearable
                  style="width: 100%"
                />
              </el-form-item>
              <el-form-item class="mr10 mb5 fl" prop="end_time">
                <el-date-picker
                  v-model="filter.end_time"
                  placeholder="结束时间"
                  type="date"
                  format="yyyy-MM-dd"
                  value-format="yyyy-MM-dd"
                  size="small"
                  clearable
                  style="width: 100%"
                />
              </el-form-item>
              <el-form-item class="mr10 mb5 fl" style="width: 300px">
                <el-button size="small" @click="app.resetFields($refs['filter'].$parent,'filter')">清 空</el-button>
                <el-button type="primary" size="small" @click="getList(getParameter())">查 询</el-button>
              </el-form-item>
            </el-form>
          </div>
        </el-col>
      </el-row>
      <el-table
          v-loading="app.loading"
          class="search-table"
          :data="list"
          border
          :summary-method="getSummaries"
          show-summary
          fit
          highlight-current-row
          style="width: 100%;"
        >
          <el-table-column label="序号" align="center" width="56px">
            <template slot-scope="{$index}">
              {{ $index + 1 }}
            </template>
          </el-table-column>
          <el-table-column label="签订公司" width="220px" :formatter="formatterCompany" />
          <el-table-column label="PO单号" width="190px">
            <template slot-scope="{row}">
              {{ 'PO'+row.pro_id }}
            </template>
          </el-table-column>
          <el-table-column label="项目名称" min-width="220px" prop="pro_name" />
          <el-table-column label="采购日期" align="center" width="140px" prop="pur_time" />
          <el-table-column label="供应商名称" min-width="220px">
            <template slot-scope="{row}">
              {{ row.sup_name ? row.sup_name : '仓库' }}
            </template>
          </el-table-column>
          <el-table-column label="采购金额" align="right" width="140px">
            <template slot-scope="{row}">
              {{ app.toFix(row.price, 2) }}
            </template>
          </el-table-column>
          <el-table-column label="已付款金额" align="right" width="140px">
            <template slot-scope="{row}">
              {{ app.toFix(row.pay, 2) }}
            </template>
          </el-table-column>
          <el-table-column label="未付款金额" align="right" width="140px">
            <template slot-scope="{row}">
              {{ app.toFix(row.price-row.pay, 2) }}
            </template>
          </el-table-column>
          <el-table-column label="追加金额" align="right" width="140px">
            <template slot-scope="{row}">
              {{ app.toFix(row.additional, 2) }}
            </template>
          </el-table-column>
          <el-table-column label="追减金额" align="right" width="140px">
            <template slot-scope="{row}">
              {{ app.toFix(row.reduction, 2) }}
            </template>
          </el-table-column>
          <el-table-column label="已开票金额" align="right" width="140px">
            <template slot-scope="{row}">
              {{ app.toFix(row.has_tax, 2) }}
            </template>
          </el-table-column>
          <el-table-column label="未开票金额" align="right" width="140px">
            <template slot-scope="{row}">
              {{ app.toFix(app.toFix(row.price, 2) - app.toFix(row.has_tax, 2), 2) }}
            </template>
          </el-table-column>
          <el-table-column label="采购类型" align="center" width="90px" :formatter="formatterType" />
          <el-table-column label="备注" min-width="220px" prop="remark" />
        </el-table>
        <pagination v-show="filter.pageAll>0" :total="filter.pageAll" :page.sync="filter.pageCurrent" :limit.sync="filter.pageNumber" @pagination="getList" />
    </div>
  </el-dialog>
</template>

<script>
import page from '@/utils/page'
import { purchases, purchaseTotalAmount } from '@/api/statistics/statistics'

export default {
  components: { },
  extends: page,
  inject: ['app'],
  props: {
    info: {
      type: Object,
      default: null
    },
    reqList: {
      default: purchases,
      type: Function
    }
  },
  data() {
    return {
      parameter: { state: 1 },
      price: {}
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.getList(this.getParameter())
      }
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  mounted() {
  },
  methods: {
    getParameter() {
      let month = this.info.edit.month
      if((this.filter.begin_time && this.filter.begin_time !== '') || (this.filter.end_time && this.filter.end_time !== '')) {
        month = ''
      }
      return {...this.parameter, ...this.filter, ...{month: month}}
    },
    formatterType(row) {
      let type = ''
      if (row.type === 1) {
        type = '物料采购'
      } else if (row.type === 2) {
        type = '设备采购'
      } else if (row.type === 3) {
        type = '工程外包'
      } else if (row.type === 4) {
        type = '库存出库'
      }
      return type
    },
    getListAfter() {
      purchaseTotalAmount(this.getParameter()).then(res => {
        this.price = res.info ? res.info : {}
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    formatterCompany(row) {
      let company = ''
      if (row.company && row.company !== '') {
        company = row.company
      } else if (row.entrust_name && row.entrust_name !== '') {
        company = row.entrust_name
      } else if (row.type === 4) {
        company = '里欧总公司'
      }
      return company
    },
    getSummaries() {
      const sums = []
      sums[0] = '合计'
      sums[1] = ''
      sums[2] = ''
      sums[3] = ''
      sums[4] = ''
      sums[5] = ''
      sums[6] = this.app.toFix(this.price.price, 2)
      sums[7] = this.app.toFix(this.price.pay, 2)
      sums[8] = this.app.toFix(this.price.price - (this.price.pay ? this.price.pay : 0), 2)
      sums[9] = this.app.toFix(this.price.additional, 2)
      sums[10] = this.app.toFix(this.price.reduction, 2)
      sums[11] = this.app.toFix(this.price.has_tax, 2)
      sums[12] = this.app.toFix(this.price.price - (this.price.has_tax ? this.price.has_tax : 0), 2)
      return sums
    }
  }
}
</script>

<style scoped>
</style>
